#
# PostGIS - Spatial Types for PostgreSQL
# http://www.postgis.net
#
# This is free software; you can redistribute and/or modify it under
# the terms of the GNU General Public Licence. See the COPYING file.
#
# PostGIS documentation build Makefile
#
# Copyright 2003-2012 Sandro Santilli <strk@kbt.io>
# Copyright 2004-2012 Paul Ramsey <pramsey@cleverelephant.ca>
# Copyright 2009-2011 Regina Obe <lr@pcorp.us>
# Copyright 2008-2010 Mark Cave-Ayland
# Copyright 2008-2010 Kevin Neufeld <kneufeld.ca@gmail.com>
# Copyright 2009-2010 Olivier Courtin <olivier.courtin@camptocamp.com>
# Copyright 2005-2006 Markus Schaber <markus@schabi.de>
#
# NOTE: We don't use a standard PGXS Makefile here since the DOCS target
# only allows files to be stored within contrib/ and we currently
# store documentation under contrib/postgis due to the possibility
# that we could produce a large number of files (think chunked HTML)
#

translations = it_IT pt_BR fr es pl ko_KR de ja

POSTGIS_MAJOR_VERSION=@POSTGIS_MAJOR_VERSION@
POSTGIS_MINOR_VERSION=@POSTGIS_MINOR_VERSION@
POSTGIS_MICRO_VERSION=@POSTGIS_MICRO_VERSION@

ifneq (,$(findstring dev,$(POSTGIS_MICRO_VERSION)))
  POSTGIS_DOWNLOAD_URL=http://postgis.net/stuff
else
  POSTGIS_DOWNLOAD_URL=http://download.osgeo.org/postgis/source
endif

INSTALL=@INSTALL@
INSTALL_DATA=@INSTALL_DATA@

XSLTPROC=@XSLTPROC@
XSLBASE=@XSLBASE@
XMLLINT=@XMLLINT@
CAN_BUILD_COMMENTS=@CAN_BUILD_COMMENTS@
PERL=@PERL@

# To allow network access use:
#
# $(MAKE) html XSLTPROCFLAGS=
#
ifeq ($(XSLTPROCFLAGS),)
	XSLTPROCFLAGS=--nonet
endif

XSLTPROC_COMMONOPTS= \
	--param section.autolabel 1 \
	--param section.label.includes.component.label 1 \
	--param chunk.section.depth 0 \
	--param generate.section.toc.level 2 \
	--param funcsynopsis.style kr \
	--param admon.graphics 1 \
	--param admon.textlabel 0 \
	--param make.clean.html 1 \
	--param simplesect.in.toc 0 \
	--param use.id.as.filename 1 \
	--param chunk.quietly 1 \
	--param header.rule 0 \
	--param footer.rule 0 \
	--param table.borders.with.css 1 \
	--stringparam chunker.output.encoding UTF-8 \
	--stringparam chunker.output.indent no \
	--stringparam saxon.character.representation decimal \
	--stringparam html.stylesheet style.css \
	$(XSLTPROCFLAGS)


HTML_DOCBOOK_XSL=$(XSLBASE)/html/docbook.xsl
CHUNK_HTML_DOCBOOK_XSL=$(XSLBASE)/html/chunk.xsl

# DBLatex's dblatex script for PDF generation from DocBook
DBLATEX=@DBLATEX@

# Imagemagick's convert utility program for generated images used in the documentation
IMAGEMAGICK=@IMAGEMAGICK@

# Gettext for translated documentation
MSGMERGE=msgmerge

# XML gettext tools
XML2POT=xml2pot

# DOCBOOK to EPUB
DBTOEPUB=dbtoepub

# Directories for postgresql subdirectories
PGSQL_DOCDIR=@PGSQL_DOCDIR@
PGSQL_MANDIR=@PGSQL_MANDIR@
PGSQL_SHAREDIR=@PGSQL_SHAREDIR@

BUILD_TARGET =

ifeq ($(CAN_BUILD_COMMENTS),yes)
BUILD_TARGET += comments
endif

ifeq ($(CAN_BUILD_PDF),yes)
BUILD_TARGET += pdf
endif

# TODO: add "html" to the BUILD_TARGET if it can be built

all: $(BUILD_TARGET)

XML_SOURCES = \
    extras_address_standardizer.xml \
	extras_historytable.xml \
	extras_tigergeocoder.xml \
	extras_topology.xml \
	extras.xml \
	faq_raster.xml \
	faq.xml \
	installation.xml \
	administration.xml \
	database_tuning.xml \
	introduction.xml \
	performance_tips.xml \
	postgis.xml \
	reference_accessor.xml \
	reference_bbox.xml \
	reference_cluster.xml \
	reference_constructor.xml \
	reference_editor.xml \
	reference_exception.xml \
	reference_guc.xml \
	reference_input.xml \
	reference_lrs.xml \
	reference_management.xml \
	reference_measure.xml \
	reference_sfcgal.xml \
	reference_operator.xml \
	reference_output.xml \
	reference_overlay.xml \
	reference_processing.xml \
	reference_raster.xml \
	reference_relationship.xml \
	reference_srs.xml \
	reference_trajectory.xml \
	reference_transaction.xml \
	reference_transformation.xml \
	reference_troubleshooting.xml \
	reference_type.xml \
	reference_validation.xml \
	reference_version.xml \
	reference.xml \
	release_notes.xml \
	reporting.xml \
	using_postgis_app.xml \
	using_postgis_dataman.xml \
	using_postgis_query.xml \
	using_raster_dataman.xml

XML_GENERATED_SOURCES = \
	postgis_aggs_mm.xml \
	postgis-out.xml \

XML_INPUTS = $(XML_SOURCES) $(XML_GENERATED_SOURCES)

XML_INPUTS_POT = $(XML_SOURCES:%.xml=po/templates/%.xml.pot)

.PHONY: $(XML_INPUTS_POT)
$(XML_INPUTS_POT): po/templates/%.xml.pot: %.xml
	@if test -f $@; then \
	  $(XML2POT) $< > $@.tmp; \
    changed=`diff $@.tmp $@ | grep -v '^[<>] "POT-Creation-Date:' | wc -l`; \
    if expr $${changed} \> 3 >/dev/null; then \
      echo "$@ updated"; \
      mv $@.tmp $@; \
    else \
      echo "$@ unchanged"; \
      rm $@.tmp; \
    fi; \
  else \
	  $(XML2POT) $< > $@; \
  fi;

# Update translation templates
update-pot: $(XML_INPUTS_POT)

# Creates or updates translation files
update-po: $(XML_INPUTS_POT)
	@for lang in $(translations); do \
    echo "Creating po files for language $$lang..." ; \
    for pot in $(XML_INPUTS_POT); do \
      mkdir -p po/$$lang; \
      po=po/$$lang/`basename $$pot .pot`.po; \
      if test -f $$po; then \
        $(MSGMERGE) --update $$po $$pot; \
      else \
        cp $$pot $$po; \
      fi; \
    done; \
  done

pull-tx:
	tx pull --all
	$(MAKE) update-po

push-tx:
	tx push -s -t --skip

# Register transifex resources, run when adding a new .xml file
update-tx:
	for f in $(XML_INPUTS_POT); do \
    f=`basename $${f} .pot`; \
    res=`echo "$${f}" | sed 's/\.//g'`pot; \
    tx set --auto-local -r postgis.$${res} "po/<lang>/$${f}.po" \
           --source-lang en --source-file "po/templates/$${f}.pot" \
           --execute; \
  done


ifeq ($(XSLTPROC),)
postgis_aggs_mm.xml: requirements_not_met_xsltproc
else
postgis_aggs_mm.xml: ./xsl/postgis_aggs_mm.xml.xsl Makefile postgis-out.xml $(XML_SOURCES)
	$(XSLTPROC) --novalid ./xsl/postgis_aggs_mm.xml.xsl postgis-out.xml > $@
endif

postgis_comments.sql: ./xsl/postgis_comments.sql.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/postgis_comments.sql.xsl postgis-out.xml > $@

postgis_cheatsheet.html: ./xsl/postgis_cheatsheet.html.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/postgis_cheatsheet.html.xsl postgis-out.xml > $@

raster_comments.sql: ./xsl/raster_comments.sql.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/raster_comments.sql.xsl postgis-out.xml > $@

raster_cheatsheet.html: ./xsl/raster_cheatsheet.html.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/raster_cheatsheet.html.xsl postgis-out.xml > $@

topology_comments.sql: ./xsl/topology_comments.sql.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/topology_comments.sql.xsl postgis-out.xml > $@

topology_cheatsheet.html: ./xsl/topology_cheatsheet.html.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/topology_cheatsheet.html.xsl postgis-out.xml > $@

sfcgal_comments.sql: ./xsl/sfcgal_comments.sql.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/sfcgal_comments.sql.xsl postgis-out.xml > $@

sfcgal_cheatsheet.html: ./xsl/sfcgal_cheatsheet.html.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/sfcgal_cheatsheet.html.xsl postgis-out.xml > $@

tiger_geocoder_comments.sql: ./xsl/tiger_geocoder_comments.sql.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/tiger_geocoder_comments.sql.xsl postgis-out.xml > $@

tiger_geocoder_cheatsheet.html: ./xsl/tiger_geocoder_cheatsheet.html.xsl $(XML_INPUTS)
	$(XSLTPROC) --novalid ./xsl/tiger_geocoder_cheatsheet.html.xsl postgis-out.xml > $@

postgis-out.xml: postgis.xml Makefile
	$(PERL) -lpe "s'@@LAST_RELEASE_VERSION@@'${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}'g;s'@@POSTGIS_DOWNLOAD_URL@@'${POSTGIS_DOWNLOAD_URL}'g;" $< > $@

chunked-html: postgis-out.xml Makefile images $(XML_INPUTS)
	$(XSLTPROC) $(XSLTPROC_COMMONOPTS) \
		--output html/ \
		$(CHUNK_HTML_DOCBOOK_XSL) \
		$<

chunked-html-web: postgis-out.xml Makefile images $(XML_INPUTS)
	$(XSLTPROC) $(XSLTPROC_COMMONOPTS) \
		--output html/ \
		--stringparam html.script ga.js \
		$(CHUNK_HTML_DOCBOOK_XSL) \
		$<

HTML_BITS = $(wildcard html/*.html)

chunked-html-web-mobile: postgis-out.xml Makefile images $(XML_INPUTS)
	$(XSLTPROC) $(XSLTPROC_COMMONOPTS) \
		--output html/ \
		--stringparam html.script ga.js \
		$(CHUNK_HTML_DOCBOOK_XSL) \
		$<
	$(PERL) -pi -e 's/<head>/<head><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">/g' $(HTML_BITS)


html: html/postgis$(DOCSUFFIX).html

html-localized:
	@for lang in $(translations); do \
    echo "Creating html for language $$lang..."; \
    $(MAKE) -C po/$$lang local-html; \
  done

html/postgis$(DOCSUFFIX).html: postgis-out.xml Makefile images $(XML_INPUTS)
	$(XSLTPROC) $(XSLTPROC_COMMONOPTS) \
		--output html/postgis$(DOCSUFFIX).html \
		$(HTML_DOCBOOK_XSL) \
		$<


postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}$(DOCSUFFIX).pdf: postgis-out.xml images $(XML_INPUTS)
	if test x"$(DBLATEX)" = x; then \
	   echo "Error: dblatex not found, can't build pdf"; \
	   echo "       try installing dblatex and then re-run configure"; \
	   false; \
	else \
		$(DBLATEX) -T native -t pdf \
		   -I "${PWD}/html" \
		   -P doc.collab.show=0 \
		   -P figure.note="${PWD}/html/images/note" \
		   -P figure.tip="${PWD}/html/images/tip" \
		   -P figure.important="${PWD}/html/images/important" \
		   -P figure.warning="${PWD}/html/images/warning" \
		   -P figure.caution="${PWD}/html/images/caution" \
		   -P latex.output.revhistory=0 \
		   -s "${PWD}/texstyle.sty" \
		   -o postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}$(DOCSUFFIX).pdf $<; \
	fi

postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}$(DOCSUFFIX).epub: postgis-out.xml images $(XML_INPUTS)
	if test x"$(DBTOEPUB)" = x; then \
	   echo "Error: dbtoepub not found, can't build epub"; \
	   echo "       try installing dbtoepub"; \
	   false; \
	else \
		$(DBTOEPUB) -c "${PWD}/html/style.css" \
		 -o postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}$(DOCSUFFIX).epub \
		  $<; \
	fi

epub: postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}$(DOCSUFFIX).epub

pdf: postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}$(DOCSUFFIX).pdf

pdf-localized:
	@for lang in $(translations); do \
    echo "Creating pdf for language $$lang..."; \
    $(MAKE) -C po/$$lang local-pdf; \
  done

doxygen.cfg: doxygen.cfg.in
	$(PERL) -lpe "s'@@LAST_RELEASE_VERSION@@'${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}'g" $< > $@

doxygen: doxygen.cfg
	doxygen $<

images:
	$(MAKE) -C html/image_src images

images-clean:
	$(MAKE) -C html/image_src images-clean

clean:
	rm -f html/*.html
	rm -f postgis-${POSTGIS_MAJOR_VERSION}.${POSTGIS_MINOR_VERSION}.${POSTGIS_MICRO_VERSION}.pdf
	rm -f *.epub
	$(MAKE) -C html/image_src clean
	rm -f $(XML_GENERATED_SOURCES)
	rm -f postgis_gardentest_${POSTGIS_MAJOR_VERSION}${POSTGIS_MINOR_VERSION}.sql
	rm -f raster_gardentest_${POSTGIS_MAJOR_VERSION}${POSTGIS_MINOR_VERSION}.sql

distclean: clean
	$(MAKE) -C html/image_src distclean
	rm -f Makefile Makefile.comments html/image_src/Makefile

maintainer-clean: clean images-clean
	rm -f postgis_comments.sql raster_comments.sql topology_comments.sql tiger_geocoder_comments.sql sfcgal_comments.sql

ifneq ($(CAN_BUILD_COMMENTS),yes)
comments:
else
comments: postgis_comments.sql raster_comments.sql topology_comments.sql sfcgal_comments.sql tiger_geocoder_comments.sql
endif

cheatsheets: postgis_cheatsheet.html raster_cheatsheet.html topology_cheatsheet.html sfcgal_cheatsheet.html tiger_geocoder_cheatsheet.html

ifneq ($(CAN_BUILD_COMMENTS),yes)
comments-install:
	if test -e postgis_comments.sql  -a \
	        -e raster_comments.sql   -a \
	        -e topology_comments.sql -a \
	        -e sfcgal_comments.sql -a \
	        -e tiger_geocoder_comments.sql; then \
	  $(MAKE) -f Makefile.comments install; \
	fi
else
comments-install: comments
	$(MAKE) -f Makefile.comments install
endif

comments-uninstall:
	$(MAKE) -f Makefile.comments uninstall

man-install: man/shp2pgsql.1 man/pgsql2shp.1
	mkdir -p $(DESTDIR)$(PGSQL_MANDIR)/man1
	$(INSTALL_DATA) man/pgsql2shp.1 $(DESTDIR)$(PGSQL_MANDIR)/man1/pgsql2shp.1
	$(INSTALL_DATA) man/shp2pgsql.1 $(DESTDIR)$(PGSQL_MANDIR)/man1/shp2pgsql.1

man-uninstall:
	rm -f $(DESTDIR)$(PGSQL_MANDIR)/man1/shp2pgsql.1
	rm -f $(DESTDIR)$(PGSQL_MANDIR)/man1/pgsql2shp.1

docs-install: html/postgis.html html/style.css
	mkdir -p $(DESTDIR)$(PGSQL_DOCDIR)/postgis/images
	$(INSTALL_DATA) html/postgis.html $(DESTDIR)$(PGSQL_DOCDIR)/postgis/
	$(INSTALL_DATA) html/style.css $(DESTDIR)$(PGSQL_DOCDIR)/postgis/
	$(INSTALL_DATA) html/images/* $(DESTDIR)$(PGSQL_DOCDIR)/postgis/images/
	$(INSTALL_DATA) ../README.postgis $(DESTDIR)$(PGSQL_DOCDIR)/postgis/README.postgis

docs-uninstall:
	rm -f $(DESTDIR)$(PGSQL_DOCDIR)/postgis/postgis.html
	rm -f $(DESTDIR)$(PGSQL_DOCDIR)/postgis/style.css
	rm -rf $(DESTDIR)$(PGSQL_DOCDIR)/postgis/images
	rm -f $(DESTDIR)$(PGSQL_DOCDIR)/postgis/README.postgis

install: comments-install

uninstall: comments-uninstall

ifeq ($(XSLTPROC),)
garden: requirements_not_met_xsltproc
else
garden:	xsl/postgis_gardentest.sql.xsl $(XML_INPUTS)
	$(XSLTPROC) -o postgis_gardentest_${POSTGIS_MAJOR_VERSION}${POSTGIS_MINOR_VERSION}.sql xsl/postgis_gardentest.sql.xsl postgis-out.xml
	$(XSLTPROC) -o raster_gardentest_${POSTGIS_MAJOR_VERSION}${POSTGIS_MINOR_VERSION}.sql xsl/raster_gardentest.sql.xsl postgis-out.xml
endif



check: check-unit
check-regress:

ifeq ($(XMLLINT),)
check-unit: requirements_not_met_xmllint
else
check-unit: $(XML_INPUTS)
	$(XMLLINT) --loaddtd --xinclude --valid postgis-out.xml > /dev/null
endif

check-localized:
	@for lang in $(translations); do \
    echo "Creating html for language $$lang..."; \
    $(MAKE) -C po/$$lang local-check; \
  done


requirements_not_met_xsltproc:
	@echo
	@echo "configure was unable to find 'xsltproc' which is required"
	@echo "to build the documentation."
	@echo "Install xsltproc and then re-run configure. Alternatively "
	@echo "refer to online manual:"
	@echo
	@echo "  http://postgis.net/documentation"
	@echo

requirements_not_met_xmllint:
	@echo
	@echo "configure was unable to find 'xmllint' which is required"
	@echo "to test the documentation."
	@echo "Install xmllint and then re-run configure. Alternatively "
	@echo "refer to online manual:"
	@echo
	@echo "  http://postgis.net/documentation"
	@echo

requirements_not_met_xslbase:
	@echo
	@echo "configure was unable to find the Docbook XSL stylesheet directory"
	@echo "which is required to build the documentation."
	@echo "Install the Docbook XSL stylesheets and/or re-run configure "
	@echo "with the --with-xsldir option."
	@echo "Alternatively refer to online manual:"
	@echo
	@echo "  http://postgis.net/documentation"
	@echo

requirements_not_met_imagemagick:
	@echo
	@echo "configure was unable to find the ImageMagick's 'convert' utility program."
	@echo "To build the documentation, install ImageMagick and then re-run configure. Alternatively "
	@echo "refer to online manual:"
	@echo
	@echo "  http://postgis.net/documentation"
	@echo
